define(function(require,exports,module) {
    var oWindow = $(window);
    var oHeader = $('.js_headerBox');                       // 头部
    var oFooter = $('.js_footer');                          // 底部

    var floatVal = 50;                                      // 误差50px


    /*
    div 在可视区域的判断
    * 1. div.offsetTop > headerHeight
    * 2. div.offsetBttom > headerHeight
    *
    * */


    var viewEl = {
        // 寻找相邻元素
        showViewEls: function (obj, fn) {
            var _this = this;
            var arrObj = [];
            var arrClass = [];
            var _children = obj.children();

            if(_children.length < 1) return;

            _children.each(function (i) {
                arrObj.push( $(_children[i]) );
                arrClass.push( _children[i].className );
            });

            // console.log('arrObj', arrObj)
            // console.log('arrClass', arrClass)


            _this.filterElement(arrObj, function (data) {
                _this.isClass(data, arrClass, function (data, dataJson) {
                    if(fn)fn(data, dataJson)
                })
            });
        },

        // 可视区的范围
        filterElement: function (arr, fn) {
            var aFilterEl = [];
            for(var i=0; i<arr.length; i++) {
                var _offsetTop = arr[i].offset().top - oWindow.scrollTop();
                var _offsetLeft = arr[i].offset().left;
                var _offsetBottom = _offsetTop + arr[i].height();
                var _offsetRight = _offsetLeft + arr[i].width();
                var _showAreaHeight = oWindow.height() - oFooter.height() - oHeader.height();
                if( _offsetTop < _showAreaHeight && _offsetBottom > oHeader.height() + floatVal && _offsetLeft >= 0 && _offsetRight - floatVal <= oWindow.width()) {
                    aFilterEl.push(arr[i])
                }
            }
            // console.log('aFilterEl', aFilterEl)
            if(fn)fn(aFilterEl);
        },

        // 判断可视区的 class
        isClass: function (arr, sClass, fn) {
            var _this = this;
            var aLink = [];
            for(var i=0; i<arr.length; i++) {

                for(var j=0; j<sClass.length; j++) {
                    if( arr[i].hasClass(sClass[j]) ) {
                        _this.getElALenght(arr[i], aLink)
                    }
                }
            }


            _this.filterElement(aLink, function (data) {
                // 最终曝光数据
                // console.log('_aLink', data)

                // 次数添加
                // $(data[1]).attr('ddj-view', 1);
                // console.log('data', data);
                var _data = [];
                var _dataJson = [];
                for(var i=0; i<data.length; i++) {
                    var $Data = $(data[i]);

                    if( $Data.attr('href') != undefined && $Data.attr('data-ddj-view') !== '1' ) {
                        var _setJson = {};
                        $Data.attr('data-ddj-view', '1');
                        _data.push(data[i]);
                        _setJson.href = data[i].attr('href');
                        _setJson.requestid = data[i].attr('requestid');
                        _setJson.goodsid = data[i].attr('goodsid');
                        _setJson.goodschannel = data[i].attr('goodschannel');
                        _setJson.bannerid = data[i].attr('bannerid');
                        _setJson.scenetype = data[i].attr('scenetype');
                        _dataJson.push(_setJson)

                    }
                }
                if(fn)fn(_data, _dataJson)
            });
        },

        // 获取a标签的数量
        getElALenght: function (el, arr) {
            el.find('a').each(function () {
                arr.push($(this));
            });
        }


    };
    exports.viewEl = viewEl;

});